<template>
    <div class="flex-r flex-start">
        <div class="padr3">
            <Button type="text" size="large" icon="md-arrow-back" shape="circle" @click="goback(-1)" ></Button>
        </div>
        <div class="flex1">
            <Card title="信息编辑器">
                <div slot="extra">
                    <Button type="text" @click="showTool = !showTool">
                        <Icon type="ios-cube" size="18" />
                    </Button>
                </div>
                <div>
                    <div class="fs24">{{id}}</div>
                    <Data ref="main" v-model="model" :config="config" :key="changed"></Data>
                </div>
                <div class="flex-r flex-start">
                    <div class="padr10"><Button type="primary" @click="save">确认</Button></div>
                    <div><Button type="default" @click="model = {}">重置</Button></div>
                </div>
                <div class="bc13 pad10 mart10">{{model}}</div>
            </Card>
        </div>
        <div class="padl10" v-show="showTool">
            <EzFormTool ref="tool" @on-feedback-cached="getCachedData" :model.sync="model" :items="config.items" :cachekey="url" @on-form-change="setForm" />
        </div>
    </div>
</template>

<script>
    import conf from './edit'
    import Data from "../../../../ez/from/Data";
    import EzFormTool from "../../../../ez/tool/EzFormTool";
    import em from "./model"
    export default {
        name: "edit_auto___DIR____EN_NAME__",
        components: {EzFormTool, Data},
        data(){
            return{
                config:conf,
                changed:"",//model组建key值，更新回导致重新渲染
                url:'',//页面url
                showTool:false,//是否显示工具箱

                //数据管理
                id:0,
                model:{},
            }
        },
        created() {
            this.url = this.$route.fullPath
            this.id = this.$route.query.id
            if (this.id !== undefined){
                this.getData()
            }
        },
        mounted() {

        },
        methods:{
            save(){
                em.save(this.model,res=>{
                    this.$Message.success(res.msg)
                    this.goback(-1)
                })
            },
            setForm(formConfig){
                //表单工具方法
                //console.log(formConfig)
                //表单工具方法
                this.$set(this.config,"form",formConfig)
            },
            getCachedData(model){
                //console.log('parent run getCachedData')
                this.model = model
                this.changed = this.$com.md5(JSON.stringify(model))
            },
            getData(){
                em.findToEdit(this.id,res=>{
                    this.model = res.data
                    this.changed = this.$com.md5(JSON.stringify(this.model))
                })
            }
        }
    }
</script>

<style scoped>

</style>